/*
package com.sgcc.gateway.filter;

import com.sgcc.common.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class RequestLoggingFilter implements GlobalFilter, Ordered {

    private static final Logger log = LoggerFactory.getLogger(RequestLoggingFilter.class);
    private static final String START_TIME = "START_TIME";

    @Autowired
    private RedisService redisService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        exchange.getAttributes().put(START_TIME, System.currentTimeMillis());//接口请求前时间
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            //接口请求完成时间
            Long startTime = exchange.getAttribute(START_TIME);
            if(null != startTime){
                long endTime = System.currentTimeMillis() - startTime;
                Map<String, Long> timeMap = redisService.getCacheObject(START_TIME);//读取缓存
                if(timeMap == null){
                    //缓存为空，初始化
                    timeMap = new HashMap<>();
                    timeMap.put("time",endTime);
                    timeMap.put("num",1L);
                }else{
                    //数值累加
                    if(timeMap.get("time") == null){
                        timeMap.put("time",endTime);
                        timeMap.put("num",1L);
                    }else{
                        timeMap.put("time",timeMap.get("time") + endTime);
                        timeMap.put("num",timeMap.get("num") + 1);
                    }
                }
                redisService.setCacheObject(START_TIME,timeMap,30L, TimeUnit.DAYS);//存入缓存
                log.info("请求接口，耗时{}毫秒",endTime);
            }
        }));
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
*/
